﻿/*-----------------------------------------------------------------------------!
! SupplierKazImpl class                                                        !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierKazImpl.cs                                                !
! Function : Implementation of SupplierKaz interface                           !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Amit Kumar Mondal                                           !
! Last changed by: Amit Kumar Mondal, May 01 2011                              !
===============================================================================*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Upadan;
using System.Data.SqlClient;
using Model.Sadharon;
using Model.Poribar.ProductPanjeri;
namespace Model.Poribar.ProductPanjeri
{
    public class ProductKazImpl : ProductKaz
    {
        private DBSadharon idGenerator;
        private Result result;
        private Product product;

        public ProductKazImpl() {
            idGenerator = new DBSadharon();
            result = new Result();
        }

        public Result getById(string id)
        {
            Supplier supplier = new Supplier();
            Result result = new Result();
           
            return result;

            
        }

        public Result create(Object productObj)
        {
            
            Product product = (Product)productObj;
            product.Id = idGenerator.getUniqId(DatabaseConnection.GetConnection(), "Product", "PR");
            string insertString = "insert into Product values(" + "'" + product.Id + "','" + product.Code + "','"+  
                product.ProductName + "','" + product.Description + "'," + product.Price+",'"+product.IsVat+
                "'," + product.Delcharge + "," +product.Quantity +",'" + product.Size+ "','" + product.Colour + "','" +
                product.isLive + "')";
            try
            {
              MakeQuery(insertString).ExecuteNonQuery();
              result.IsSuccess = true;
             }catch(Exception e){
                 result.IsSuccess = false;
            }
            finally {
                DatabaseConnection.sqlConnect.Close();
           }
            return result;
        }

        public Result All() {
            string findString = "select * from Product where Live='L'";
            try
            {
                SqlDataReader productReader = MakeQuery(findString).ExecuteReader();
                if (productReader.HasRows)
                {
                    List<Product> allProducts = new List<Product>();
                    while (productReader.Read())
                    {
                        product = new Product();
                        product.Code = productReader["Code"].ToString();
                        product.ProductName = productReader["Name"].ToString();
                        product.Id = productReader["Id"].ToString();
                        product.Description = productReader["Description"].ToString();
                        product.Price = float.Parse(productReader["Price"].ToString());
                        product.Delcharge = float.Parse(productReader["DelCharge"].ToString());
                        product.Quantity = int.Parse(productReader["Quantity"].ToString());
                        product.Colour = productReader["ColourId"].ToString();
                        product.Size = productReader["SizeId"].ToString();
                        product.IsVat = (char)productReader["Vat"].ToString().ToCharArray()[0];
                        product.isLive = (char)productReader["Live"].ToString().ToCharArray()[0];
                        allProducts.Add(product);
                    }
                    result.ResultObj = allProducts;
                    result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
            }
            finally
            {
                DatabaseConnection.sqlConnect.Close();
            }
            return result;
        }
        public Result update(Object _product) {

            Result result = new Result();
            return result;
        }
        public Result delete(string _id) {

            Result result = new Result();
            return result;
        }

        private SqlCommand MakeQuery(string queryString) 
        {
            DatabaseConnection.GetConnection().Open();
            return (new SqlCommand(queryString, DatabaseConnection.sqlConnect));
        }

    }
}
